package com.thoughtworks;

import java.util.Scanner;

/**
 * Created by cmi on 7/22/14.
 * driven by BibliotecaTest.
 */

public final class Biblioteca {
    private static Biblioteca instance = null;
    private final String version = "1.0";
    private Database database = new Database("book");
    public String[] header = new String[]{"id","name","author","year","status"};
    public String[][] books = new String[][]{
            {"00000001","C++ Primer","Stanley B. Lippman","2005",""},
            {"00000002","Think in Java","Bruce Eckel","2002",""},
            {"00000003","Ruby on Rails","Bruce Tate","2006",""},
            {"00000004","Refactoring Improving the Design of Existing Code","Martin Fowler","1999",""}};

    public static void main(String[] args) {
        Biblioteca.getInstance().run();
    }

    private Biblioteca(){}

    public static Biblioteca getInstance() {
        if(instance==null) {
            synchronized (Biblioteca.class) {
                instance = new Biblioteca();
            }
        }
        return instance;
    }

    public void run() {
        if (!database.Login("star", "1234")) {
            initFailure();
            return;
        } else {
            welcome();
        }
        Scanner scanner = new Scanner(System.in);
        scanner.useDelimiter("\n");
        while(scanner.hasNext()) {
            String command = scanner.next();
            switchCommand(command);
        }
        listBook();
    }

    private void switchCommand(String command) {
        switch(command) {
            case "":
                break;
            case "1":
            case "l":
            case "list":
                listBook();
                break;
            case "2":
            case "c":
            case "checkoutBook":
                checkoutBook();
                break;
            case "3":
            case "r":
            case "return":
                returnBook();
                break;
            case "0":
            case "q":
            case "quit":
                System.out.println("bye");
                System.exit(0);
                break;
            default:
                choice();
                System.out.println("Select a valid option!");
                break;
        }
    }

    private void checkoutBook() {
        System.out.print("id? ");
        Scanner scanner = new Scanner(System.in);
        scanner.useDelimiter("\n");
        if (scanner.hasNext()) {
            String id = scanner.next();
            for(String[] book:books) {
                if(book[0].equals(id) && book[4].equals("")) {
                    book[4] = "checkedOut";
                    System.out.println("Thank you! Enjoy the book.");
                    return;
                }
            }
            System.out.println("That book is not available.");
        }
    }

    private void returnBook() {
        System.out.print("id? ");
        Scanner scanner = new Scanner(System.in);
        scanner.useDelimiter("\n");
        if (scanner.hasNext()) {
            String id = scanner.next();
            for(String[] book:books) {
                if(book[0].equals(id) && !book[4].equals("")) {
                    book[4] = "";
                    System.out.println("Thank you for returning the book.");
                    return;
                }
            }
            System.out.println("That is not a valid book to return.");
        }
    }

    private void choice() {
        System.out.println("1.List - book list");
        System.out.println("2.Checkout - checkoutBook book");
        System.out.println("3.return - return book");
        System.out.println("0.Quit - quit system");
    }

    private void listBook() {
        final int maxWidth = 25;
        int[] rows = rows(maxWidth);
        final String[] mask = mask(rows, " ");
        final String[] mask_ = mask(rows, "_");

        printItems(header, mask_);
        for(String[] book : books) {
            if(book[4].equals(""))
                printItems(book, mask);
        }

    }

    private int[] rows(int maxWidth) {
        int[] rows = new int[header.length];
        {
            int i=0;
            for (String head : header) {
                if (head.length() > rows[i]) {
                    rows[i] = head.length() < maxWidth ? head.length() : maxWidth;
                }
                ++i;
            }
        }
        for(String[] book : books) {
            int i=0;
            for (String value:book) {
                if(value.length()>rows[i]) {
                    rows[i] = value.length() < maxWidth ? value.length() : maxWidth;
                }
                ++i;
            }
        }
        return rows;
    }

    private String[] mask(int[] rows, String mask) {
        String[] masks = new String[rows.length];
        int i=0;
        for(int row:rows) {
            String space = "";
            for(int j=0;j<row;++j) {
                space += mask;
            }
            space = space.substring(0,row);
            masks[i++] = space;
        }
        return masks;
    }

    private void printItems(String[] items, String[] mask) {
        int i=0;
        for(String item:items) {
            StringBuilder buff= new StringBuilder(mask[i]);
            buff.replace(0, item.length(), item);
            int row = mask[i].length();
            if(item.length()> row) {
                buff.setLength(row-3);
                System.out.print(buff+"...|");
            } else {
                buff.setLength(row);
                System.out.print(buff+"|");
            }
            ++i;
        }
        System.out.println("");
    }

    private void welcome() {
        System.out.println("Welcome to the Biblioteca, version-" + getVersion());
    }

    private void initFailure() {
        System.out.println("Biblioteca init failure.");
    }

    public String getVersion() {
        return version;
    }
}
